﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Data
{
    /// <summary>
    /// 数据公共类
    /// </summary>
    public class DataCommon
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public DataCommon() { }


        #region 全局数据变量
        /// <summary>
        /// 数据库连接语句
        /// </summary>
        public static string SqlString;
        /// <summary>
        /// SqlConnection连接
        /// </summary>
        public static SqlConnection SqlConnection1;
        /// <summary>
        /// SqlDataReader连接
        /// </summary>
        public static SqlDataReader SqlDataReader1;
        /// <summary>
        /// 获取单据类型SqlDataReader
        /// </summary>
        public static SqlDataReader VchtypeSqlDataReader;
        #endregion

        #region SQL语句头标志
        /// <summary>
        /// SQL语句头标志
        /// </summary>
        public struct SqlSign
        {
            /// <summary>
            /// SQL插入标志
            /// </summary>
            public static string Insert = "Insert ";
            /// <summary>
            /// SQL删除标志
            /// </summary>
            public static string Delete = "Delete ";
            /// <summary>
            /// SQL查询标志
            /// </summary>
            public static string Select = "Select ";
            /// <summary>
            /// SQL查询影响行标志
            /// </summary>
            public static string SelectCount = "Select Count ";

        }
        #endregion

        #region 生成SQL连接语句
        /// <summary>
        /// 生成SQL连接语句
        /// </summary>
        /// <param name="DataAddress">数据库地址</param>
        /// <param name="DataName">数据库名称</param>
        /// <param name="DataAccounts">数据库帐号</param>
        /// <param name="DataPassword">数据库密码</param>
        /// <param name="Checking">认证方式</param>
        /// <returns></returns>
        public static string CreateSqlString(string DataAddress, string DataName, string DataAccounts, string DataPassword, bool Checking)
        {
            try
            {
                string SqlString;
                if (Checking == true)
                {
                    SqlString = @"Data Source=" + DataAddress + @";Initial Catalog=" + DataName + @";Integrated Security=SSPI";
                    return SqlString;
                }
                else
                {
                    SqlString = @"Data Source=" + DataAddress + @";Initial Catalog=" + DataName + @";User ID=" + DataAccounts + ";Pwd=" + DataPassword;
                    return SqlString;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 登陆验证
        /// <summary>
        /// 登陆验证
        /// </summary>
        /// <param name="SqlString">Sql连接字符串</param>
        /// <returns></returns>
        public static bool Landing(string SqlString)
        {
            bool returns = false;
            try
            {
                SqlConnection1 = new SqlConnection(SqlString);
                SqlConnection1.Open();
                //SqlDataReader1 = cmd1.ExecuteReader();
                returns = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return returns;
        }
        #endregion

        #region 获取SqlDataReader数据源
        /// <summary>
        /// 获取SqlDataReader数据源
        /// </summary>
        /// <param name="SqlString">Sql连接字符串</param>
        /// <param name="SqlSentence">Sql查询语句</param>
        /// <returns></returns>
        public static SqlDataReader Get_SqlDataReader(string SqlString, string SqlSentence)
        {
            try
            {
                SqlConnection1 = new SqlConnection(SqlString);
                SqlCommand cmd1 = new SqlCommand(SqlSentence, SqlConnection1);
                SqlConnection1.Open();
                SqlDataReader1 = cmd1.ExecuteReader();
                return SqlDataReader1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取执行SQL语句受影响行数
        /// <summary>
        /// 获取执行SQL语句受影响行数
        /// </summary>
        /// <param name="SqlString">Sql连接字符串</param>
        /// <param name="SqlSentence">Sql查询语句</param>
        /// <returns></returns>
        public static int Get_SqlDataReaderCount(string SqlString, string SqlSentence)
        {
            try
            {
                int returns;
                SqlConnection1 = new SqlConnection(SqlString);
                SqlCommand cmd1 = new SqlCommand(SqlSentence, SqlConnection1);
                SqlConnection1.Open();
                returns = cmd1.ExecuteNonQuery();
                MessageBox.Show(returns.ToString());
                SqlConnection1.Close();
                return returns;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 关闭SqlDataReader和SqlConnection
        /// <summary>
        /// 关闭SqlDataReader和SqlConnection
        /// </summary>
        /// <returns></returns>
        public static bool SqlClose()
        {
            try
            {
                SqlDataReader1.Close();
                SqlConnection1.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
                throw ex;
            }
        }
        #endregion

    }
}
